package com.zpark.sports.controller.exception;


import com.zpark.sports.service.exception.BasicServletException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.Map;

@ControllerAdvice
public class GlobalExceptionHandlerController{
//    @ExceptionHandler(Exception.class)
//    @ResponseBody
//    public Map<String,Object> handlerException(Throwable e){
//        //定义返回的数据类型
//        Map<String,Object> map=new HashMap<>();
//        //封装错误信息数据
//        map.put("code",10086);
//        map.put("mag",e.getMessage());
//        return map;
//
//    }

    @ExceptionHandler(BasicServletException.class)
    public ModelAndView handlerException(Throwable e){
        System.err.println(e.getMessage());


        //创建视图解析对象
        ModelAndView mv=new ModelAndView();
        //设置视图解析器
        mv.setViewName("500");

        //定义返回的数据类型
        Map<String,Object> map=new HashMap<>();
        //封装错误信息数据
        map.put("code",10086);
        map.put("mag",e.getMessage());
        //绑定数据
        mv.addObject("result",map);


        return mv;
    }


}
